package com.wjqy.controller;

import com.wjqy.entity.User;
import com.wjqy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@SessionAttributes({"userId"})
public class TestController {

    @Autowired
    private UserService userService;


    @RequestMapping("say")
    public List<User> test2(){
        List<User> all = userService.findAll();
        return all;
    }

    @RequestMapping("say/hello")
    public String test3(){
        return "hello world";
    }


    @RequestMapping("fileupload")
    public String uploadFile(MultipartFile file, HttpServletRequest req) throws IOException {
        // 判断文件是否为空，空则返回失败页面
        if (file.isEmpty()) {
            return "failed";
        }
        // 获取文件存储路径（绝对路径）
        String path = req.getServletContext().getRealPath("/WEB-INF/pages/static/images");
        // 获取原文件名
        File dir = new File(path);
        if (!dir.exists()){
            dir.mkdirs();
        }
        String fileName = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
        fileName = uuid+"_"+fileName;
        // 创建文件实例
        File filePath = new File(path, fileName);
        // 写入文件
        file.transferTo(filePath);
        return "success";
    }

    @RequestMapping("fileupload2")
    public String uploadFile2(@RequestParam("file") MultipartFile[] files, HttpServletRequest req) throws IOException {
        // 判断文件是否为空，空则返回失败页面
        if (files.length==0) {
            return "failed";
        }
        // 获取文件存储路径（绝对路径）
        String path = req.getServletContext().getRealPath("/WEB-INF/pages/static/images");
        // 获取原文件名
        File dir = new File(path);
        if (!dir.exists()){
            dir.mkdirs();
        }
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
            fileName = uuid+"_"+fileName;
            // 创建文件实例
            File filePath = new File(path, fileName);
            // 写入文件
            file.transferTo(filePath);
        }
        return "success";
    }


    @RequestMapping("setuserid")
    public String setSession(Model model,HttpSession session){
        model.addAttribute("userId",1);
        return "session设置成功";
    }

    @RequestMapping("getuserid")
    public String getSession(HttpServletRequest request, ModelMap modelMap, HttpSession session){
        String userid = String.valueOf(request.getSession().getAttribute("userid"));
        String userid2 = String.valueOf(modelMap.get("userid"));
        String userid1 = String.valueOf(session.getAttribute("userid"));
        return userid.toString()+"-------"+userid1+">>>>>>>>>"+userid2;
    }

}
